import axios from 'axios'

// 实例化
const instance = axios.create({
  baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net/',
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(config => {
  return config
}, e => Promise.reject(e))

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response.data
  },
  // 状态码不在 200-300
  (error) => {
    return Promise.reject(error)
  }
)

function createAPI (url, method, data) {
  return instance({
    url,
    method,
    [method.toLowerCase() === 'get' ? 'params' : 'data']: data
  })
}

export default createAPI
